import { ref } from 'vue'
import { defineStore } from 'pinia'
interface UserInfo {
  userNmae: string
  [key: string]: any
}
export const useUserInfo = defineStore('userInfo', () => {
  const token = ref<string | null>(localStorage.getItem('token'))
  const userInfo = ref<Object | null>(JSON.parse(localStorage.getItem('userInfo') || '{}'))
  const setUserInfo = ({ token: Token, ...user }: UserInfo) => {
    localStorage.setItem('userInfo', JSON.stringify(user))
    Token && localStorage.setItem('token', Token as string)
    userInfo.value = user
    token.value = Token
  }
  const removeUserInfo = (): void => {
    token.value = null
    userInfo.value = null
    localStorage.clear()
  }
  return { token, userInfo, setUserInfo, removeUserInfo }
})
